<template>
  <view class="comBox">
    <sickTitleCom></sickTitleCom>
    <!-- 血压 -->
    <inputComSelectNoNextBtn @choseList="choseList"></inputComSelectNoNextBtn>
    <!-- 血糖 -->
    <inputComSelectFBGNoNextBtn @choseList="choseList"></inputComSelectFBGNoNextBtn>
    <!-- 血脂 -->
    <inputComSelectTCHONoNextBtn @choseList="choseList"></inputComSelectTCHONoNextBtn>
    <!-- 肾功能 -->
    <inputComSelectMAUNoNextBtn @choseList="choseList"></inputComSelectMAUNoNextBtn>
    <!-- 幽门螺杆菌 -->
    <radioComNoNextBtn :info="youMenData" @choseList="choseList"></radioComNoNextBtn>

    <!-- 下一题上一题按钮 -->
    <nextProBtnCom @nextBtn="nextBtn" @proBtn="proBtn"></nextProBtnCom>
  </view>
</template>

<script>
/* eslint-disabled */
import nextProBtnCom from '@/components/nextProButton';
import radioComNoNextBtn from './radioComNoNextBtn';
import inputComSelectNoNextBtn from './inputComSelectNoNextBtn';
import inputComSelectFBGNoNextBtn from './inputComSelectFBGNoNextBtn';
import inputComSelectTCHONoNextBtn from './inputComSelectTCHONoNextBtn';
import inputComSelectMAUNoNextBtn from './inputComSelectMAUNoNextBtn';
import sickTitleCom from './sickTitleCom';
import { ajaxSend } from './ajaxHealth.js';
import curIcon from '@/static/icon/cur.png';
import { getData } from '@/utils/optimize';
import { checkInt, checkBetween, checkBetweenDecimalOne, decimalOne, decimalTwo } from '@/common/assessKeyCheck';
export default {
  components: {
    // 上一题下一题组件
    nextProBtnCom,
    // 疾病大类title
    sickTitleCom,
    radioComNoNextBtn,

    inputComSelectNoNextBtn,
    inputComSelectFBGNoNextBtn,
    inputComSelectTCHONoNextBtn,
    inputComSelectMAUNoNextBtn
  },

  data() {
    return {
      checkValue: {},
      curIcon: curIcon,
      noChose: false,
      validateObjs: {},
      youMenData: {
        key: 'hPylori',
        name: '是否进行过幽门螺杆菌检查？若做过，结果如何？',
        pageNum: 23, // 在第几页展示
        options: [
          { name: '阳性', id: 10, tips: '' },
          { name: '阴性', id: 20, tips: '' },
          { name: '没测过', id: 30, tips: '', isPassCheck: true }
        ],
        tips: '',
        value: ''
      },
      mainData: {}
    };
  },
  computed: {
    active() {
      return this.$store.getters.active;
    },
    assessForm() {
      return this.$store.getters.assessForm;
    },
    uSex() {
      return this.$store.getters.userSex;
    },
    isHaveDiabetes() {
      return this.$store.getters.isHaveDiabetes;
    }
  },
  methods: {
    choseList(key, value) {
      // value 为键值对，validate为校验函数
      if (key === 'hPylori') {
        // 幽门螺杆菌传过来的是函数
        this.mainData[key] = value()[key];
      } else {
        this.mainData[key] = value;
      }
    },
    validateXueYa() {
      // 校验血压
      if (this.mainData['SBP'] === undefined || this.mainData['DBP'] === undefined) {
        uni.showToast({ title: `请填写血压值`, icon: 'none' });
        return false;
      }
      // ②录入值格式校验
      if (checkInt(this.mainData.SBP)) {
        uni.showToast({ title: `高压数据错误，请重新输入`, icon: 'none' });
        return false;
      }
      if (checkBetween(this.mainData.SBP, '高压', 50, 250)) {
        uni.showToast({
          title: checkBetween(this.mainData.SBP, '高压', 50, 250),
          icon: 'none'
        });
        return false;
      }
      if (checkInt(this.mainData.DBP)) {
        uni.showToast({ title: `低压数据错误，请重新输入`, icon: 'none' });
        return false;
      }
      if (checkBetween(this.mainData.DBP, '低压', 30, 250)) {
        uni.showToast({
          title: checkBetween(this.mainData.DBP, '低压', 50, 250),
          icon: 'none'
        });
        return false;
      }
      return true;
    },
    validateXueTang() {
      // 血糖校验
      if (this.isHaveDiabetes) {
        if (!this.mainData.FBG) {
          uni.showToast({ title: `请输入空腹血糖`, icon: 'none' });
          return false;
        }
        if (!this.mainData.PBG) {
          uni.showToast({ title: `请输入餐后2小时血糖`, icon: 'none' });
          return false;
        }
      } else {
        // ②录入值格式校验
        if (checkBetweenDecimalOne(this.mainData.FBG, '空腹血糖', 0.1, 15)) {
          uni.showToast({ title: `空腹血糖数据错误，请重新输入`, icon: 'none' });
          return false;
        }
        if (checkBetweenDecimalOne(this.mainData.PBG, '餐后两小时血糖', 0.1, 15)) {
          uni.showToast({
            title: `餐后两小时血糖数据错误，请重新输入`,
            icon: 'none'
          });
          return false;
        }
      }

      if (decimalOne(this.mainData.HBALC)) {
        uni.showToast({
          title: `糖化血红蛋白数据错误，请重新输入`,
          icon: 'none'
        });
        return false;
      }
      if (checkBetween(this.mainData.HBALC, '糖化血红蛋白', 3, 20)) {
        uni.showToast({
          title: `糖化血红蛋白数据错误，请重新输入`,
          icon: 'none'
        });
        return false;
      }
      return true;
    },

    validateXueZhi() {
      // 血脂校验
      if (checkBetween(this.mainData.TCHO, '总胆固醇', 0.01, 15)) {
        uni.showToast({ title: `总胆固醇数据错误，请重新输入`, icon: 'none' });
        return false;
      }
      if (checkBetween(this.mainData.TG, '甘油三酯', 0.01, 12)) {
        uni.showToast({ title: `甘油三酯数据错误，请重新输入`, icon: 'none' });
        return false;
      }
      if (checkBetween(this.mainData.HDL, '高密度脂蛋白胆固醇', 0.01, 3)) {
        uni.showToast({
          title: `高密度脂蛋白胆固醇数据错误，请重新输入`,
          icon: 'none'
        });
        return false;
      }
      if (checkBetween(this.mainData.LDL, '低密度脂蛋白胆固醇', 0.01, 6)) {
        uni.showToast({
          title: `低密度脂蛋白胆固醇数据错误，请重新输入`,
          icon: 'none'
        });
        return false;
      }
      return true;
    },
    validateShen() {
      // ②录入值格式校验
      if (checkBetween(this.mainData.MAU, '尿微量白蛋白', 0.01, 2000)) {
        uni.showToast({
          title: `尿微量白蛋白数据错误，请重新输入`,
          icon: 'none'
        });
        return false;
      }
      if (this.mainData.uricAcid && checkInt(this.mainData.uricAcid)) {
        uni.showToast({ title: `尿酸数据错误，请重新输入`, icon: 'none' });
        return false;
      }
      if (checkBetween(this.mainData.uricAcid, '尿微量白蛋白', 30, 5000)) {
        uni.showToast({ title: `尿酸数据错误，请重新输入`, icon: 'none' });
        return false;
      }
      // 血肌酐校验
      if (decimalTwo(this.mainData.Scr)) {
        uni.showToast({ title: `血肌酐数据错误，请重新输入`, icon: 'none' });
        return false;
      }
      let xjgChecke = false;
      // if (this.index === '1') {
      if (this.mainData.newScrUnit === 2) {
        xjgChecke = checkBetween(this.mainData.Scr, '血肌酐', 0.06, 56);
      } else {
        xjgChecke = checkBetween(this.mainData.Scr, '血肌酐', 5, 5000);
      }
      if (xjgChecke) {
        uni.showToast({ title: `血肌酐数据错误，请重新输入`, icon: 'none' });
        return false;
      }
      return true;
    },

    async nextBtn() {
      /* eslint-disable */
      let submitData = this.assessForm;

      if (!this.validateXueYa() || !this.validateXueTang() || !this.validateXueZhi() || !this.validateShen()) {
        return;
      }

      if (this.mainData['hPylori'] === undefined) {
        uni.showToast({ title: `请选择幽门螺杆菌检查情况`, icon: 'none' });
        return;
      }

      console.log('this.mainData:', this.mainData);
      let healthData = await ajaxSend(this.mainData);
      console.log('healthData:', healthData);
      const xueTangTips = [
        {
          nameKey: 'FBG',
          title: '空腹血糖',
          healthGuidances: ['信息缺失，无法给出评估结论']
        },
        {
          nameKey: 'PBG',
          title: '餐后2小时血糖',
          healthGuidances: ['信息缺失，无法给出评估结论']
        },
        {
          nameKey: 'HBALC',
          title: '糖化血红蛋白',
          healthGuidances: ['信息缺失，无法给出评估结论']
        }
      ];
      xueTangTips.forEach((xzObj) => {
        if (!this.mainData[xzObj.nameKey]) {
          healthData.push(xzObj);
        }
      });
      const xueZhiTips = [
        {
          nameKey: 'TCHO',
          title: '总胆固醇',
          healthGuidances: ['信息缺失，无法给出评估结论']
        },
        {
          nameKey: 'TG',
          title: '甘油三酯',
          healthGuidances: ['信息缺失，无法给出评估结论']
        },
        {
          nameKey: 'HDL',
          title: '高密度脂蛋白胆固醇',
          healthGuidances: ['信息缺失，无法给出评估结论']
        },
        {
          nameKey: 'LDL',
          title: '低密度脂蛋白胆固醇',
          healthGuidances: ['信息缺失，无法给出评估结论']
        }
      ];
      xueZhiTips.forEach((xzObj) => {
        if (!this.mainData[xzObj.nameKey]) {
          healthData.push(xzObj);
        }
      });
      const shenTips = [
        {
          nameKey: 'MAU',
          title: '尿微量白蛋白',
          healthGuidances: ['信息缺失，无法给出评估结论']
        },
        {
          nameKey: 'uricAcid',
          title: '尿酸',
          healthGuidances: ['信息缺失，无法给出评估结论']
        },
        {
          nameKey: 'Scr',
          title: '血肌酐',
          healthGuidances: ['信息缺失，无法给出评估结论']
        }
      ];
      shenTips.forEach((xzObj) => {
        if (!this.mainData[xzObj.nameKey]) {
          healthData.push(xzObj);
        }
      });
      // const youMenTips = [
      //   {
      //     nameKey: 'hPylori',
      //     title: '幽门螺杆菌',
      //     healthGuidances: ['建议作为常规体检项目及早检查。']
      //   }
      // ];

      // youMenTips.forEach((xzObj) => {
      //   if (this.mainData[xzObj.nameKey] === 30) {
      //     healthData.push(xzObj);
      //   }
      // });
      submitData = Object.assign(submitData, this.mainData);
      submitData.physicalExaminationDate = getData();
      if (healthData && healthData.length > 0) {
        // 如果有异常，则提示
        this.$store.commit('assessQuestion/SET_AJAXRESMESSAGE', healthData); // 存提示信息到vux，数组格式
        this.$store.commit('assessQuestion/SET_AJAXRES', true); // 开启提示
        wx.pageScrollTo({
          scrollTop: 0
        });
      } else {
        // this.$store.commit('assessQuestion/SET_ACTIVE', this.active + 1);
        this.$store.commit('assessQuestion/SET_AJAXRES', false);
      }
      // 获取健康指标指导意见 判断是出健康提示还是直接下一步 end
      this.$store.commit('assessQuestion/SET_ASSESSFORM', submitData);
      wx.pageScrollTo({
        scrollTop: 0
      });
    },
    proBtn() {
      this.$store.commit('assessQuestion/SET_ACTIVE', 3);
    }
  }
};
</script>
<style lang="scss">
.comBox {
  .title {
    background: rgba(216, 246, 214, 1);
    color: rgba(0, 0, 0, 0.55);
    font-size: 32rpx;
    padding: 14rpx;
    text-align: center;
  }
}
.clear {
  clear: both;
  overflow: hidden;
  zoom: 1;
  height: 0px;
}
.lineHeight86 {
  height: 86rpx;
  line-height: 86rpx;
}
.size28 {
  font-size: 28rpx;
}
.titleBold {
  font-weight: 500;
}
</style>
